#include "sdk.h"
#include "M_Code_statusbar.h"

BEGIN_EVENT_TABLE( M_Code_StatusBar, wxStatusBar )
  EVT_SIZE( M_Code_StatusBar::OnSize )
END_EVENT_TABLE()


M_Code_StatusBar::M_Code_StatusBar( wxWindow* parent, M_Code_unused wxWindowID id, long style, const wxString& name )
  : wxStatusBar( parent, wxID_ANY, style, name ) {
}

M_Code_StatusBar::~M_Code_StatusBar() {
}

void M_Code_StatusBar::SetStatusTextByPlugin( const wxString& text, M_Code_Plugin* plugin ) {
  int pos = GetFieldNumberOfPlugin( plugin );
  if( pos != -1 ) {
    wxStatusBar::SetStatusText( text, pos );
  }
}

wxString M_Code_StatusBar::GetStatusTextByPlugin( M_Code_Plugin* plugin ) const {
  int pos = GetFieldNumberOfPlugin( plugin );
  if( pos != -1 ) {
    return wxStatusBar::GetStatusText( pos );
  }
  return wxEmptyString;
}

bool M_Code_StatusBar::GetFieldRectByPlugin( M_Code_Plugin* plugin, wxRect& rect ) const {
  int pos = GetFieldNumberOfPlugin( plugin );
  if( pos != -1 ) {
    return wxStatusBar::GetFieldRect( pos, rect );
  }
  return false;
}

void M_Code_StatusBar::SetStatusWidths( int n, const int* widths ) {
  if( widths ) {
    m_MainWidths.clear();
    for( int i = 0 ; i < n ; i++ ) {
      m_MainWidths.push_back( widths[i] );
    }
    UpdateWidths();
  }
}

void M_Code_StatusBar::AddField( M_Code_Plugin* plugin, wxWindow* ctrl, int width ) {
  cbStatusBarElement elem;
  elem.control = ctrl;
  elem.width = width;
  elem.plugin =  plugin;
  m_Elements.push_back( elem );
  UpdateWidths();
  return;
}

void M_Code_StatusBar::AddField( M_Code_Plugin* plugin, int width ) {
  return AddField( plugin, ( wxControl* )NULL, width );
}

void M_Code_StatusBar::RemoveField( M_Code_Plugin* plugin ) {
  for( ElementVector::iterator it = m_Elements.begin() ; it != m_Elements.end() ; it++ ) {
    if( it->plugin == plugin ) {
      if( it->control ) {
        it->control->Destroy();
      }
      m_Elements.erase( it );
      break;
    }
  }
  UpdateWidths();
}

void M_Code_StatusBar::OnSize( wxSizeEvent& event ) {
  AdjustFieldsSize();
  event.Skip();
}

void M_Code_StatusBar::UpdateWidths() {
  unsigned int n = m_MainWidths.size() + m_Elements.size();
  int *widths = new int[n];
  unsigned int i = 0;
  for( ; i < m_MainWidths.size() ; i++ ) {
    widths[i] = m_MainWidths[i];
  }
  for( int k = 0 ; i < n ; i++, k++ ) {
    widths[i] = m_Elements[k].width;
  }
  wxStatusBar::SetFieldsCount( n );
  wxStatusBar::SetStatusWidths( n, widths );
  delete[] widths;
}

void M_Code_StatusBar::AdjustFieldsSize() {
  for( unsigned int i = 0 ; i < m_Elements.size(); i++ ) {
    wxWindow *ctrl = m_Elements[i].control;
    if( ctrl ) {
      wxRect rect;
      GetFieldRectByPlugin( m_Elements[i].plugin, rect );
      ctrl->SetSize( rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4 );
    }
  }
}

int M_Code_StatusBar::GetFieldNumberOfPlugin( M_Code_Plugin* plugin ) const {
  for( unsigned int i = 0 ; i < m_Elements.size() ; i++ ) {
    if( m_Elements[i].plugin == plugin ) {
      return i + m_MainWidths.size();
    }
  }
  return -1;
}
